/*action create by pionner code builder*/
package com.pioneer.app.sms.jiaotong;

import org.apache.log4j.Logger;

import java.io.Serializable;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletResponseAware;
import org.dom4j.Document;
import org.hibernate.Transaction;

import com.app.util.Dom4jUtil;
import com.opensymphony.xwork2.Action;
import com.pioneer.app.dev.department.DepartmentDAO;

public class NormalSmsAction implements ServletResponseAware, Action, Serializable {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(NormalSmsAction.class);

	// key
	private java.lang.Integer id;

	private java.lang.Integer[] ids;
	private NormalSms normalsms;
	private java.lang.String title;
	private java.lang.String content;
	private List objects=null;
	private String condition=null;
	private String fromTree=null;
	private HttpServletResponse response;
	/** 默认构造方法 */
	public NormalSmsAction(){
	}
	/** full constructor */
	public NormalSmsAction(java.lang.String title,java.lang.String conten){
		this.title=title;
		this.content=content;

	}
	public void setId(java.lang.Integer id){
		if (logger.isDebugEnabled()) {
			logger.debug("setId(java.lang.Integer) - start");
		}
		this.id=id;

		if (logger.isDebugEnabled()) {
			logger.debug("setId(java.lang.Integer) - end");
		}
	}
	public java.lang.Integer getId(){
		if (logger.isDebugEnabled()) {
			logger.debug("getId() - start");
		}

		if (logger.isDebugEnabled()) {
			logger.debug("getId() - end");
		}
		return id;
	}

	public void setIds(java.lang.Integer[] ids){
		if (logger.isDebugEnabled()) {
			logger.debug("setIds(java.lang.Integer[]) - start");
		}
		this.ids=ids;

		if (logger.isDebugEnabled()) {
			logger.debug("setIds(java.lang.Integer[]) - end");
		}
	}
	public java.lang.Integer[] getIds(){
		if (logger.isDebugEnabled()) {
			logger.debug("getIds() - start");
		}

		if (logger.isDebugEnabled()) {
			logger.debug("getIds() - end");
		}
		return ids;
	}

	public void setNormalSms(NormalSms normalsms){
		if (logger.isDebugEnabled()) {
			logger.debug("setNormalSms(NormalSms) - start");
		}
		this.normalsms=normalsms;

		if (logger.isDebugEnabled()) {
			logger.debug("setNormalSms(NormalSms) - end");
		}
	}
	public NormalSms getNormalSms(){
		if (logger.isDebugEnabled()) {
			logger.debug("getNormalSms() - start");
		}

		if (logger.isDebugEnabled()) {
			logger.debug("getNormalSms() - end");
		}
		return normalsms;
	}

	public void setTitle(java.lang.String title){
		if (logger.isDebugEnabled()) {
			logger.debug("setTitle(java.lang.String) - start");
		}
		this.title=title;

		if (logger.isDebugEnabled()) {
			logger.debug("setTitle(java.lang.String) - end");
		}
	}
	public java.lang.String getTitle(){
		if (logger.isDebugEnabled()) {
			logger.debug("getTitle() - start");
		}

		if (logger.isDebugEnabled()) {
			logger.debug("getTitle() - end");
		}
		return title;
	}

	public void setContent(java.lang.String content){
		if (logger.isDebugEnabled()) {
			logger.debug("setContent(java.lang.String) - start");
		}
		this.content=content;

		if (logger.isDebugEnabled()) {
			logger.debug("setContent(java.lang.String) - end");
		}
	}
	public java.lang.String getContent(){
		if (logger.isDebugEnabled()) {
			logger.debug("getContent() - start");
		}

		if (logger.isDebugEnabled()) {
			logger.debug("getContent() - end");
		}
		return content;
	}

	public List getObjects() {
		if (logger.isDebugEnabled()) {
			logger.debug("getObjects() - start");
		}

		if (logger.isDebugEnabled()) {
			logger.debug("getObjects() - end");
		}
		return objects;
	}

	public void setObjects(List objects) {
		if (logger.isDebugEnabled()) {
			logger.debug("setObjects(List) - start");
		}
		this.objects = objects;

		if (logger.isDebugEnabled()) {
			logger.debug("setObjects(List) - end");
		}
	}

	public String getCondition() {
		if (logger.isDebugEnabled()) {
			logger.debug("getCondition() - start");
		}

		if (logger.isDebugEnabled()) {
			logger.debug("getCondition() - end");
		}
		return condition;
	}

	public void setCondition(String condition) {
		if (logger.isDebugEnabled()) {
			logger.debug("setCondition(String) - start");
		}
		this.condition = condition;

		if (logger.isDebugEnabled()) {
			logger.debug("setCondition(String) - end");
		}
	}

	public String execute() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("execute() - start");
		}

		if (logger.isDebugEnabled()) {
			logger.debug("execute() - end");
		}
		return "SUCESS";
	}

	public String doList(){
		if (logger.isDebugEnabled()) {
			logger.debug("doList() - start");
		}
		NormalSmsDAO dao=new NormalSmsDAO();
		this.objects=dao.findByCondition(condition);

		if (logger.isDebugEnabled()) {
			logger.debug("doList() - end");
		}
		return "list";
	}

	public String doAdd(){
		if (logger.isDebugEnabled()) {
			logger.debug("doAdd() - start");
		}

		if (logger.isDebugEnabled()) {
			logger.debug("doAdd() - end");
		}
		return "add";
	}

	public String doEdit(){
		if (logger.isDebugEnabled()) {
			logger.debug("doEdit() - start");
		}
		NormalSmsDAO dao=new NormalSmsDAO();
		this.normalsms=dao.findById(id);
		dao.getSession().close();
		this.sedObj();;

		if (logger.isDebugEnabled()) {
			logger.debug("doEdit() - end");
		}
		return "edit";
	}

	public String doSave(){
		if (logger.isDebugEnabled()) {
			logger.debug("doSave() - start");
		}
		Transaction tx=null;
		try {
			this.buildObj();
			NormalSmsDAO dao=new NormalSmsDAO();
			tx=dao.getSession().beginTransaction();
			if(null==this.normalsms.getId()){
//			insert a new object
			//gave the object id by database 
				dao.add(normalsms);
			}else{
//			update the object
				dao.getSession().clear();
				dao.update(normalsms);
			}
			tx.commit();
			dao.getSession().close();
		} catch (Exception e) {
			logger.error("doSave()", e);
			tx.rollback();
			e.printStackTrace();

			if (logger.isDebugEnabled()) {
				logger.debug("doSave() - end");
			}
			return "error";
		}
	

		if (logger.isDebugEnabled()) {
			logger.debug("doSave() - end");
		}
		return "list";
	}

	public String doDetail(){
		if (logger.isDebugEnabled()) {
			logger.debug("doDetail() - start");
		}
			NormalSmsDAO dao=new NormalSmsDAO();
		this.normalsms=dao.findById(id);
		dao.getSession().close();
		this.sedObj();;
		if(null!=fromTree){;

			if (logger.isDebugEnabled()) {
				logger.debug("doDetail() - end");
			}
			return "detail";
		}else{

			if (logger.isDebugEnabled()) {
				logger.debug("doDetail() - end");
			}
			return "list";
		}
	}

	public String doSelWin(){
		if (logger.isDebugEnabled()) {
			logger.debug("doSelWin() - start");
		}
		
		try {
			response.setHeader("Pragma","No-cache");
			response.setHeader("Cache-Control","no-cache");
			response.setDateHeader("Expires",1); 
			condition="title like'%"+title+"%'";
			
			NormalSmsDAO dao=new NormalSmsDAO();
			Document doc=dao.findSelWinDoc(condition);
			
			dao.getSession().close();
			this.response.setContentType("text/xml");
			Dom4jUtil.writeDocToOut(doc, "utf-8", this.response.getOutputStream());
		} catch (Exception e) {
			logger.error("doSelWin()", e);

			e.printStackTrace();
		}

		if (logger.isDebugEnabled()) {
			logger.debug("doSelWin() - end");
		}
	return null;
	}
	public String doDelete(){
		if (logger.isDebugEnabled()) {
			logger.debug("doDelete() - start");
		}
		try{
			NormalSmsDAO dao=new NormalSmsDAO();
			dao.deleteObjs(ids);
			dao.getSession().close();

			if (logger.isDebugEnabled()) {
				logger.debug("doDelete() - end");
			}
			return "list";
		}catch(Exception e){
			logger.error("doDelete()", e);

			if (logger.isDebugEnabled()) {
				logger.debug("doDelete() - end");
			}
			return "error";
		}
	}

	private void buildObj(){
		if (logger.isDebugEnabled()) {
			logger.debug("buildObj() - start");
		}
		this.normalsms=new NormalSms();
		this.normalsms.setId(this.id);
		this.normalsms.setTitle(this.title);
		this.normalsms.setContent(this.content);

		if (logger.isDebugEnabled()) {
			logger.debug("buildObj() - end");
		}
	}
	private void sedObj(){
		if (logger.isDebugEnabled()) {
			logger.debug("sedObj() - start");
		}

		if(null!=this.normalsms){
			this.id=this.normalsms.getId();
			this.title=this.normalsms.getTitle();
			this.content=this.normalsms.getContent();

		}

		if (logger.isDebugEnabled()) {
			logger.debug("sedObj() - end");
		}
	}
	public void setServletResponse(HttpServletResponse arg0) {
		if (logger.isDebugEnabled()) {
			logger.debug("setServletResponse(HttpServletResponse) - start");
		}

		// TODO Auto-generated method stub
		this.response=arg0;
		

		if (logger.isDebugEnabled()) {
			logger.debug("setServletResponse(HttpServletResponse) - end");
		}
	}
}